package org.alwayssuper.web.endpoint;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.alwayssuper.netmc.core.annotation.Endpoint;
import org.alwayssuper.netmc.core.annotation.Mapping;
import org.alwayssuper.netmc.session.Session;
import org.alwayssuper.protocol.commons.GBTRequestType;
import org.alwayssuper.protocol.gbt32960.*;
import org.alwayssuper.web.model.entity.DeviceDO;
import org.alwayssuper.web.model.enums.SessionKey;
import org.springframework.stereotype.Component;

/**
 * GBT32960协议端点处理器
 * 优化版本：消除冗余代码，利用拦截器机制
 * @author alwaysSuper
 */
@Slf4j
@Endpoint
@Component
@RequiredArgsConstructor
public class GBT32960Endpoint {


    @Mapping(types = GBTRequestType.车辆登入, desc = "车辆登入")
    public void G01(G01 message, Session session) {
        session.register(message);
        
        // 创建设备信息
        DeviceDO device = new DeviceDO();
        device.setDeviceId(message.getClientId());
        device.setProtocolVersion(message.getProtocolVersion());
        session.setAttribute(SessionKey.Device, device);
        // 返回void，让拦截器的successful方法处理响应
    }

    @Mapping(types = GBTRequestType.实时信息上报, desc = "实时信息上报")
    public void G02(G02 message, Session session) {
        
        // TODO: 保存实时数据到数据库或推送到消息队列

    }

    @Mapping(types = GBTRequestType.车辆登出, desc = "车辆登出")
    public void G04(G04 message, Session session) {
        // 不在这里立即断开连接，由拦截器在应答发送后延迟断开
        log.info("{}车辆登出消息处理完成，等待应答发送后断开连接", session);
    }

    @Mapping(types = GBTRequestType.数据单元加密密钥交换, desc = "数据单元加密密钥交换")
    public void G0B(CommonResponse message, Session session) {
        session.response(message);
    }
}
